local channi = fk.CreateSkill {

  name = "joy__channi",

  tags = {  },

}



channi:addEffect("active", {
  name = "joy__channi",
  anim_type = "support",
  prompt = "#channi-active",
  min_card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(channi.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) ~= Card.PlayerEquip
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local n = #effect.cards
    room:moveCardTo(effect.cards, Player.Hand, target, fk.ReasonGive, channi.name, nil, false, player.id)
    room:setPlayerMark(target, channi.name, n)
    local success, data = room:askForUseActiveSkill(target, "joy__channi_viewas", "#joy__channi-invoke:"..player.id.."::"..n, true, {}, false)
    room:setPlayerMark(target, channi.name, 0)
    if success then
      local card = Fk:cloneCard("duel")
      card.skillName = channi.name
      card:addSubcards(data.cards)
      local use = {
        from = target.id,
        tos = table.map(data.targets, function(id) return {id} end),
        card = card,
        extra_data = {channi_data = {player.id, target.id, #data.cards}}
      }
      room:useCard(use)
    end
  end,
})

channi:addEffect(fk.Damage, {
  name = "#joy__channi_delay",
  events = {fk.Damage, fk.DamageInflicted},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead and target and not target.dead and data.card and not data.chain and
    table.contains(data.card.skillNames, channi.name) then
      local room = player.room
      local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if not card_event then return false end
      local use = card_event.data
      if use.extra_data then
        local channi_data = use.extra_data.channi_data
        if channi_data and channi_data[1] == player.id and channi_data[2] == target.id then
          event:setCostData(self, channi_data[3])
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:notifySkillInvoked(player, channi.name, "drawcard")
      room:doIndicate(player.id, {target.id})
      room:drawCards(target, event:getCostData(self), channi.name)
    else
      local cards = player.player_cards[Player.Hand]
      if #cards > 0 then
        if room:askForSkillInvoke(player,channi.name,data,"#joy__channi_damaged") then
          room:notifySkillInvoked(player, "joy__channi", "negative")
          room:throwCard(cards,channi.name,player)
          return true
        end
      end
    end
  end
})
channi:addEffect(fk.DamageInflicted, {
  name = "#joy__channi_delay",
  events = {fk.Damage, fk.DamageInflicted},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead and target and not target.dead and data.card and not data.chain and
    table.contains(data.card.skillNames, channi.name) then
      local room = player.room
      local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if not card_event then return false end
      local use = card_event.data[1]
      if use.extra_data then
        local channi_data = use.extra_data.channi_data
        if channi_data and channi_data[1] == player.id and channi_data[2] == target.id then
          event:setCostData(self, channi_data[3])
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:notifySkillInvoked(player, channi.name, "drawcard")
      room:doIndicate(player.id, {target.id})
      room:drawCards(target, event:getCostData(self), channi.name)
    else
      local cards = player.player_cards[Player.Hand]
      if #cards > 0 then
        if room:askForSkillInvoke(player,channi.name,data,"#joy__channi_damaged") then
          room:notifySkillInvoked(player, "joy__channi", "negative")
          room:throwCard(cards,channi.name,player)
          return true
        end
      end
    end
  end
})

return channi